<script lang="ts" setup>
import {
  getOneByCondition,
  getReadonly,
  insertOne,
  reSetDialogModel,
  updateOne,
  urls
} from '@/index';

const dialogModel = defineModel<DialogModel<AsmePersonInfo>>('dialogModel', {
  required: true
});
import type { AsmePersonInfo, DialogModel } from '@/index.d';
const { insertForbidden, updateForbidden } = getReadonly(dialogModel.value);
const formData = reactive<AsmePersonInfo>({
  id: 0
});

const setAsmePersonInfo = async (res: AsmePersonInfo) => {
  formData.id = res.id;
  formData.name = res.name;
  formData.username = res.username;
  formData.workNo = res.workNo;
  formData.creator = res.creator;
  formData.createTime = res.createTime;
  formData.updater = res.updater;
  formData.updateTime = res.updateTime;
  formData.rtTime = res.rtTime;
  formData.utTime = res.utTime;
  formData.mtTime = res.mtTime;
  formData.ptTime = res.ptTime;
  formData.ltTime = res.ltTime;
  formData.etTime = res.etTime;
  formData.vtTime = res.vtTime;
  formData.user = res.user;
  formData.totalTime = res.totalTime;
};

const reSetAsmePersonInfo = () => {
  reSetDialogModel(dialogModel);
  formData.id = 0;
};

watch(
  () => dialogModel.value.visible,
  value => {
    // 当打开窗口时，将父元素传递过来的主键填充到表单中去
    value && (formData.id = dialogModel.value.id as number);
    // 当关闭窗口时，重置表单中的数据
    !value && reSetAsmePersonInfo();
  },
  {
    immediate: true
  }
);

// 监控id的变化，当id为0，为新增窗口，无需从数据库取值填充，当id不为0时，为更新或者查看窗口，从数据库取值进行填充
watch(
  () => formData.id,
  async (value: number | undefined) => {
    if (value) {
      const res = await getOneByCondition<AsmePersonInfo>(urls.asmePersonInfo, 'id', value);
      res && setAsmePersonInfo(res);
    }
  },
  {
    immediate: true
  }
);

const saveFormData = async () => {
  if (dialogModel.value.id === 0) {
    const res = (await insertOne(urls.asmePersonInfo, formData)) as AsmePersonInfo;
    if (res) {
      setAsmePersonInfo(res);
      dialogModel.value.id = res.id as number;
      dialogModel.value.type = 'update';
      dialogModel.value.change = true;
    }
  } else {
    const tempFormData: AsmePersonInfo = {
      id: formData.id,
      rtTime: formData.rtTime,
      utTime: formData.utTime,
      mtTime: formData.mtTime,
      ptTime: formData.ptTime,
      etTime: formData.etTime,
      ltTime: formData.ltTime,
      vtTime: formData.vtTime
    };

    const res = (await updateOne<AsmePersonInfo>(
      urls.asmePersonInfo,
      tempFormData
    )) as AsmePersonInfo;

    if (res) {
      dialogModel.value.change = true;
      formData.updater = res.updater;
      formData.updateTime = res.updateTime;
      formData.totalTime = res.totalTime;
    }
  }
};
</script>
<template>
  <HqDialogDetail :dialog-model="dialogModel" @submit="saveFormData">
    <template #default>
      <el-card shadow="hover">
        <template #header>基本信息</template>
        <template #default>
          <hq-form :model="formData" ref="asmePersonInfoFormRef">
            <el-row :gutter="5">
              <hq-col>
                <el-form-item label="序号" prop="id">
                  <el-input v-model.number="formData.id" readonly />
                </el-form-item>
              </hq-col>
              <hq-col>
                <el-form-item label="人员" prop="name">
                  <el-select
                    v-model="formData.username"
                    placeholder="请选择人员"
                    required
                    :disabled="insertForbidden"
                  >
                    <HqUserOptions />
                  </el-select>
                </el-form-item>
              </hq-col>
              <hq-col>
                <el-form-item label="毕业学校" prop="name">
                  <el-input :value="formData.user?.graduateSchool" readonly />
                </el-form-item>
              </hq-col>
              <hq-col>
                <el-form-item label="专业" prop="major">
                  <el-input :value="formData.user?.major" readonly />
                </el-form-item>
              </hq-col>
              <hq-col>
                <el-form-item label="毕业时间" prop="graduateDate">
                  <el-input :value="formData.user?.graduateDate" readonly />
                </el-form-item>
              </hq-col>
              <hq-col>
                <el-form-item label="学历" prop="educationLevel">
                  <el-input :value="formData.user?.educationLevel" readonly />
                </el-form-item>
              </hq-col>
            </el-row>
          </hq-form>
        </template>
      </el-card>
      <el-card shadow="hover">
        <template #header>工作时长</template>
        <template #default>
          <hq-form :model="formData">
            <el-row :gutter="5">
              <hq-col>
                <el-form-item label="射线时长" prop="rtTime">
                  <el-input-number
                    :min="0"
                    :max="99999"
                    v-model="formData.rtTime"
                    :readonly="updateForbidden"
                  />
                </el-form-item>
              </hq-col>
              <hq-col>
                <el-form-item label="超声时长" prop="utTime">
                  <el-input-number
                    :min="0"
                    :max="99999"
                    v-model="formData.utTime"
                    :readonly="updateForbidden"
                  />
                </el-form-item>
              </hq-col>
              <hq-col>
                <el-form-item label="磁粉时长" prop="mtTime">
                  <el-input-number
                    :min="0"
                    :max="99999"
                    v-model="formData.mtTime"
                    :readonly="updateForbidden"
                  />
                </el-form-item>
              </hq-col>
              <hq-col>
                <el-form-item label="渗透时长" prop="ptTime">
                  <el-input-number
                    :min="0"
                    :max="99999"
                    v-model="formData.ptTime"
                    :readonly="updateForbidden"
                  />
                </el-form-item>
              </hq-col>
              <hq-col>
                <el-form-item label="涡流时长" prop="etTime">
                  <el-input-number
                    :min="0"
                    :max="99999"
                    v-model="formData.etTime"
                    :readonly="updateForbidden"
                  />
                </el-form-item>
              </hq-col>
              <hq-col>
                <el-form-item label="泄漏时长" prop="ltTime">
                  <el-input-number
                    :min="0"
                    :max="99999"
                    v-model="formData.ltTime"
                    :readonly="updateForbidden"
                  />
                </el-form-item>
              </hq-col>
              <hq-col>
                <el-form-item label="目视时长" prop="vtTime">
                  <el-input-number
                    :min="0"
                    :max="99999"
                    v-model="formData.vtTime"
                    :readonly="updateForbidden"
                  />
                </el-form-item>
              </hq-col>
              <hq-col>
                <el-form-item label="射线时长" prop="totalTime">
                  <el-input v-model="formData.totalTime" readonly>
                    <template #append>小时</template>
                  </el-input>
                </el-form-item>
              </hq-col>
            </el-row>
          </hq-form>
        </template>
      </el-card>
    </template>
  </HqDialogDetail>
</template>
<style lang="scss" scoped></style>
